﻿using Pay.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Pay.DTO;
using Pay.TO;
using Pay.Token.Utils;
using System.Threading.Tasks;
using Pay.Core;

namespace Pay.Token.Controllers {
    /// <summary>
    /// 我的订单
    /// </summary>
    [AppAuthorize("Member")]
    public class MyOrderController : GeneralController {
        private IOrderService _iOrderService;
        private ISettingService _iSettingService;
        private ILogisticsDetailsService _LogisticsDetailsService;
        private IShopInfoService _iShopInfoService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="iOrderService"></param>
        /// <param name="iSettingService"></param>
        /// <param name="LogisticsDetailsService"></param>
        /// <param name="iShopInfoService"></param>
        public MyOrderController(IOrderService iOrderService, ISettingService iSettingService, 
            ILogisticsDetailsService LogisticsDetailsService, IShopInfoService iShopInfoService) {
            _iOrderService = iOrderService;
            _iSettingService = iSettingService;
            _LogisticsDetailsService = LogisticsDetailsService;
            _iShopInfoService = iShopInfoService;
        }

        #region 买家-订单列表
        /// <summary>
        /// 订单列表页面
        /// </summary>
        /// <param name="wxOpenId"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult OrderList(string wxOpenId) {
            ViewBag.WXOpenId = wxOpenId;
            return View();
        }
        /// <summary>
        /// 订单列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public PartialViewResult _OrderList(OrderQuery query) {
            query.user_id = UserId;
            var list = _iOrderService.Get(query);
            ViewBag.img_url = RequestMethod.token_imgurl;
            return PartialView(list);
        }
        /// <summary>
        /// 取消订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult CanelOrder(long id) {
            var model = _iOrderService.CloseOrder(id);
            return Json(ResultToJson.Json(model >= 0 ? " 取消成功" : "取消失败", model), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 订单详情
        /// </summary>
        /// <param name="order_id"></param>
        /// <param name="wxOpenId"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult OrderDetail(long order_id, string wxOpenId) {
            var model = _iOrderService.Get(order_id);
            var shop = _iShopInfoService.GetShopById(model.merchant_id);
            if (shop == null || shop.id == 0)
                ViewBag.ServicePhone = _iSettingService.GetTokenSetting().app.service_phone;
            else
                ViewBag.ServicePhone = shop.customer_tel;
            ViewBag.WXOpenId = wxOpenId;
            ViewBag.img_url = RequestMethod.token_imgurl;
            return View(model);
        }
        /// <summary>
        /// 订单物流
        /// </summary>
        /// <param name="order_id"></param>
        /// <returns></returns>
        [AllowAnonymous]//允许匿名购买用户查看物流信息
        public ActionResult OrderLogistics(long order_id) {
            var model = _iOrderService.Get(order_id);
            var data = _LogisticsDetailsService.GetList(model);
            var result = new order_logistic_results();
            result.order_id = model.id;
            result.ship_order_number = model.shipping.ship_order_number;
            result.express_company_name = model.shipping.express_company_name;
            result.logistic_results = data;
            return View(result);
        }
        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult DeleteOrder(long id) {
            var model = _iOrderService.DeleteOrder(id);
            return Json(ResultToJson.Json(model >= 0 ? " 删除成功" : "删除失败", model), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 确认收货
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ConfirmOrder(long id) {
            var model = _iOrderService.ConfirmOrder(id);
            return Json(ResultToJson.Json(model >= 0 ? " 确认收货成功" : "确认收货失败", model), JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 卖家/分销商-订单列表
        /// <summary>
        /// 卖家订单列表
        /// </summary>
        /// <param name="isOnlyShowDistribution">类型（1：只显示分销订单）</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult SellerOrderList(int isOnlyShowDistribution = 0) {
            ViewBag.IsOnlyShowDistribution = isOnlyShowDistribution;
            List<DropdownItem> expressList = RequestMethod.express.ToObject<List<DropdownItem>>();
            ViewBag.ExpressList = expressList;
            return View();
        }
        /// <summary>
        /// 卖家订单列表
        /// </summary>
        /// <param name="query"></param>
        /// <param name="isOnlyShowDistribution"></param>
        /// <returns></returns>
        [HttpPost]
        public PartialViewResult _SellerOrderList(OrderQuery query, int isOnlyShowDistribution) {
            query.distribution_id = UserId;
            if (isOnlyShowDistribution != 1) {
                query.merchant_id = UserId;
                var shopInfo = _iShopInfoService.GetShopById(UserId);
                if (shopInfo != null && shopInfo.id > 0)
                    query.shop_id = shopInfo.id;
            }
            var list = _iOrderService.Get(query);
            ViewBag.img_url = RequestMethod.token_imgurl;
            ViewBag.CurUserId = UserId;
            return PartialView(list);
        }
        /// <summary>
        /// 卖家订单详情
        /// </summary>
        /// <param name="order_id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult SellerOrderDetail(long order_id) {
            List<DropdownItem> expressList = RequestMethod.express.ToObject<List<DropdownItem>>();
            var model = _iOrderService.Get(order_id);
            ViewBag.ExpressList = expressList;
            ViewBag.CurUserId = UserId;
            ViewBag.img_url = RequestMethod.token_imgurl;
            return View(model);
        }

        /// <summary>
        /// 发货-页面
        /// </summary>
        /// <param name="order_id"></param>
        /// <returns></returns>
        public ActionResult SellerDelivery(long order_id) {
            List<DropdownItem> expressList = RequestMethod.express.ToObject<List<DropdownItem>>();
            var model = _iOrderService.Get(order_id);
            ViewBag.ExpressList = expressList;
            ViewBag.CurUserId = UserId;
            ViewBag.img_url = RequestMethod.token_imgurl;
            return View(model);
        }
        /// <summary>
        /// 发货
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult OrderDelivery(order_dto_ex dto) {
            if (string.IsNullOrEmpty(dto.ship_order_number))
                throw new ServerErrorException("请输入快递单号");
            var result = _iOrderService.OrderDelivery(dto);
            return Json(ResultToJson.Json(result > 0 ? " 发货成功" : "发货失败", result), JsonRequestBehavior.AllowGet);
        }
        #endregion


        /// <summary>
        /// 收益列表查看-分销订单详情（只能进行查看，不能对订单进行操作）
        /// </summary>
        /// <param name="order_id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult DistributionOrderDetail(long order_id) {
            var model = _iOrderService.Get(order_id);
            var shop = _iShopInfoService.GetShopById(model.merchant_id);
            if (shop == null || shop.id == 0)
                ViewBag.ServicePhone = _iSettingService.GetTokenSetting().app.service_phone;
            else
                ViewBag.ServicePhone = shop.customer_tel;
            return View(model);
        }

    }
}